package dailyBlock

import (
	"gitee.com/wchpub/fnsvr"
	"gitee.com/wchpub/fnsvr/funcSless"
	"gitee.com/wchpub/xeSvrPub"
	"gitee.com/wchpub/xeSvrPub/gameProto"
)

type BlockType string

const (
	//数据块名称
	BlockName string = "daily"
)

const (
	//每日免费游戏次数
	MAX_FREE_GAME_TIMES int = 3
)

// 每日刷新的数据

const (
	//档案版本号
	Key_Ver BlockType = funcSless.GAME_DATA_VERSION_KEY

	Key_RefreshTime      BlockType = "refreshTime"      //数据刷新时间
	Key_Ticket           BlockType = "ticket"           //每日关卡入场券数量
	Key_MaxFreeGameTimes BlockType = "maxFreeGameTimes" //本日最多可免费玩的次数
	Key_PassTimes        BlockType = "passTimes"        //本日通关次数
	Key_GameTimes        BlockType = "gameTimes"        //本日游戏次数
	Key_PassReward       BlockType = "passReward"       //今日通关奖励 1-12
	Key_EditInfoTimes    BlockType = "editInfoTimes"    //今日编辑信息次数
	Key_JoinRankCtx      BlockType = "joinRankCtx"      //加入排行榜的上下文数据，可加入排行榜时存在
)

type Data struct {
	super *funcSless.Block[BlockType]
}

// 数据刷新时间
func (m *Data) RefreshTime() (int64, error) {
	return m.super.GetInt64(Key_RefreshTime)
}

type IAutoRefreshFuncs interface {
}

// 加入排行榜的上下文数据，可加入排行榜时存在
func (m *Data) SetJoinRankCtx(v *gameProto.JoinRankCtx) error {
	return m.super.SetJson(Key_JoinRankCtx, v)
}

func (m *Data) JoinRankCtx() (*gameProto.JoinRankCtx, error) {
	re := gameProto.JoinRankCtx{}
	err := m.super.GetJson(Key_JoinRankCtx, &re)
	if err != nil {
		return nil, err
	}

	return &re, nil
}

// 自动刷新数据
func (m *Data) AutoRefresh(now int64, themeId int, funcs IAutoRefreshFuncs) error {

	//themeId := pub.ThemeIdByTimestamp(now)

	m.super.PreLoad(Key_RefreshTime)

	refreshTime, err := m.RefreshTime()
	if err != nil {
		return err
	}

	if refreshTime != 0 && fnsvr.IsSameDay(xeSvrPub.Location, refreshTime, now) {
		return nil
	}

	if now < refreshTime {
		return nil
	}

	/*
		skins, err := funcs.Skins()
		if err != nil {
			return err
		}*/

	m.super.SetInt64(Key_RefreshTime, now)
	m.super.SetInt(Key_MaxFreeGameTimes, MAX_FREE_GAME_TIMES)
	m.SetTicket(MAX_FREE_GAME_TIMES)
	m.SetPassTimes(0)
	m.SetGameTimes(0)
	m.SetEditInfoTimes(0)
	m.super.SetString(Key_JoinRankCtx, "")

	/*
		//找到和当前主题相关的所有皮肤
		themeSkins := lo.Filter(skins, func(s *gameProto.SkinInfo, _ int) bool {
			return int(s.Id) == themeId
		})

		//找到点亮的所有皮肤
		lightSkins := lo.Filter(themeSkins, func(s *gameProto.SkinInfo, _ int) bool {
			return s.State == gameProto.CollectedState_Light
		})

		r := rand.New(rand.NewSource(fnsvr.GetMilliTimestamp()))

		if len(lightSkins) == 0 {
			//全部已经获取了

			m.SetPassReward(r.Intn(int(xeSvrPub.MAX_BALLNO)) + 1)
		} else {
			//从已点亮皮肤中随机一个

			m.SetPassReward(int(lightSkins[r.Intn(len(lightSkins))].Idx))

		}*/

	return nil
}

// 通关次数
func (m *Data) PassTimes() (int, error) {
	return m.super.GetInt(Key_PassTimes)
}

func (m *Data) SetPassTimes(v int) {
	m.super.SetInt(Key_PassTimes, v)
}

// 游戏次数
func (m *Data) GameTimes() (int, error) {
	return m.super.GetInt(Key_GameTimes)
}

func (m *Data) SetGameTimes(v int) {
	m.super.SetInt(Key_GameTimes, v)
}

// 日常关卡入场券数量
func (m *Data) Ticket() (int, error) {
	return m.super.GetInt(Key_Ticket)
}

func (m *Data) SetTicket(v int) {
	m.super.SetInt(Key_Ticket, v)
}

// 今日通关奖励
func (m *Data) PassReward() (int, error) {
	return m.super.GetInt(Key_PassReward)
}

func (m *Data) SetPassReward(v int) {
	m.super.SetInt(Key_PassReward, v)
}

// 今日编辑信息次数
func (m *Data) EditInfoTimes() (int, error) {
	return m.super.GetInt(Key_EditInfoTimes)
}

func (m *Data) SetEditInfoTimes(v int) {
	m.super.SetInt(Key_EditInfoTimes, v)
}

/*
// 通关奖励候选列表
func (m *Data) RewardList() ([]int, error) {
	re := []int{}
	err := m.super.GetJson(Key_RewardList, &re)
	if err != nil {
		return nil, err
	}

	return re, nil
}

func (m *Data) SetRewardList(v []int) {
	err := m.super.SetJson(Key_RewardList, v)
	if err != nil {
		fnsvr.Log().Error("dailyBlock.SetRewardList", "v", v, "err", err)
	}
}*/

func (m *Data) PreLoad(keys ...BlockType) {
	m.super.PreLoad(keys...)
}

func (m *Data) GetAll() (map[string]string, error) {
	return m.super.GetAll()
}

func (m *Data) GetString(key BlockType) (string, error) {
	return m.super.GetString(key)
}

// 获取数据块
func GetBlock(plyData *funcSless.PlayerData) (*Data, error) {

	bk, err := funcSless.GetBlock[BlockType](plyData, BlockName)
	if err != nil {
		return nil, err
	}

	return &Data{
		super: bk,
	}, nil
}
